<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // 定义一个全局变量
    var message = "this is a global variable"
    console.log(message)

    // 定义在代码块中的变量也能被外部访问
    {
      var foo1 = "this is a variable in block"
      //es6之后 使用let定义的变量具有块级作用域,外部访问不到
      let foo2 = "this is a variable in block definded by let"
    }
    console.log(foo1)
    // console.log(foo2)

    for (var i = 0; i < 10; i++) {
      var foo3 = "this is a variable in for block"
    }
    console.log(i)
    console.log(foo3)

    // 函数中定义的变量 外部不能访问
    function bar(){
      var foo4 = "this is a variable in function"
    }

    console.log(foo4)
  </script>
</body>
</html>